#ipa打包上传
: '
被注释的多行内容
'
SPACE="=============="
#Config Color
RED="${SPACE}\033[0;31m"
GREEN="${SPACE}\033[0;32m"
NC="\033[0m" # No Color

if [ ! -d ./IPADir ];then
mkdir -p IPADir;
fi

#工程绝对路径
project_path=$(cd `dirname $0`; pwd)

#工程名workspace
workspace_name=$(ls | grep xcworkspace)

#工程名
project_name=$(ls ${workspace_path} | grep xcodeproj | awk -F.xcodeproj '{print $1}')

#scheme名 将XXX替换成自己的sheme名
scheme_name=${project_name}

#打包模式 Debug/Release
development_mode=Debug

#build文件夹路径
build_path=${project_path}/build

#plist文件所在路径
exportOptionsPlistPath=${project_path}/${project_name}/Info.plist

#plist文件所在路径
exportOptionsPlistPath=${project_path}/${project_name}/Info.plist

echo "${GREEN}请选择发布环境 [ 1:app-store 2:ad-hoc] ${NC}${SPACE}"

##
read number
while([ $number != 1 ] && [ $number != 2 ])
do
echo "${RED}请确认发布环境 【1:app-store】 or 【2:ad-hoc】${NC}${SPACE}"
read number
done

if [ $number == 1 ];then
    development_mode=Release
else
    echo "${GREEN}请选择编译环境 [ 1:Release 2:Debug] ${NC}${SPACE} "
    ##
    read development
    while([ $development != 1 ] && [ $development != 2 ])
    do
    echo "${RED}请确认编译环境 【1:Release】 or 【2:Debug】${NC}${SPACE}"
    read development
    done
    if [ $development == 1 ];then
    development_mode=Release
    else
    development_mode=Debug
    fi
fi


#echo '///-----------'
#echo '/// 正在清理工程'
#echo '///-----------'
#xcodebuild \
#clean -configuration ${development_mode} -quiet  || exit
#
#echo '///--------'
#echo '/// 清理完成'
#echo '///--------'
#echo ''

echo '///-----------'
echo '/// 正在编译工程:'${development_mode}
echo '///-----------'
xcodebuild \
archive -workspace ${project_path}/${project_name}.xcworkspace \
-scheme ${scheme_name} \
-configuration ${development_mode} \
-archivePath ${build_path}/${project_name}.xcarchive  -quiet  || exit

echo '///--------'
echo '/// 编译完成'
echo '///--------'
echo ''
#导出ipa文件所在路径
exportIpaPath=${project_path}/IPADir/${development_mode}
echo '///----------'
echo '/// 开始ipa打包'
echo '///----------'
xcodebuild -exportArchive -archivePath ${build_path}/${project_name}.xcarchive \
-configuration ${development_mode} \
-exportPath ${exportIpaPath} \
-exportOptionsPlist ${exportOptionsPlistPath} \
-quiet || exit

if [ -e $exportIpaPath/$scheme_name.ipa ]; then
    echo '///----------'
    echo '/// ipa包导出成功'
    echo '///----------'
    #打开文件目录
    #open $exportIpaPath
else
    echo '///-------------'
    echo '/// ipa包导出失败 '
    echo '///-------------'
fi

echo '///------------'
echo '/// 打包ipa完成  '
echo '///-----------='
echo ''

echo '///-------------'
echo '/// 开始发布ipa包 '
echo '///-------------'

if [ $number == 1 ];then
    #验证并上传到App Store
    #用户和访问申请秘钥后下载秘钥文件.p8需要放在根目录中（例：/Users/用户名/private_keys/AuthKey_JCAUHGWJ5H.p8）
    username="开发者账号用户和访问中申请秘钥的（Issuer ID）"
    password="密钥 ID"
    
    echo "${GREEN}请选择使用账号 [ 1:郝柏 2:赵泽成 3:启业云 4:自定义] ${NC}${SPACE} "
    ##
    read account
    while([ $account != 1 ] && [ $account != 2 ] && [ $account != 3 ] && [ $account != 4 ])
    do
    echo "${RED}请先选择账号${NC}${SPACE}"
    read account
    done
    
    case $account in
        1)
        username="78aec493-e014-4bce-944d-4a7a7db1260e"
        password="JCCUHGWJ6H"
        ;;
        2)
        username=""
        password=""
        ;;
        3)
        username=""
        password=""
        ;;
        *)
        username=""
        password=""
    esac

    if [ -z "$username" ]; then
        echo -e "${RED}用户名不能为空${NC}🌧🌧🌧"
        read aid
        while([ -z "$aid" ])
        do
        echo "${RED}请输入用户名${NC}${SPACE}"
        read aid
        done
        username=$aid
    fi

    if [ -z "$password" ]; then
        echo -e "${RED}密码不能为空${NC}🌧🌧🌧"
        stty -echo#密文输入开始
        read apw
        while([ -z "$apw" ])
        do
        echo "${RED}请输入密码${NC}${SPACE}"
        read apw
        done
        password=$apw
        stty echo#密文输入结束
    fi

    echo "===账号:$username ===密码:$password"
    
    #2个命令分别对应 验证IPA 和 上传IPA，都需要使用3个参数ipa路径、apiKey、apiIssuer
    #$ altool --validate-app -f file -u username [-p password] [--output-format xml]
    #$ altool --upload-app -f file -u username [-p password] [--output-format xml]
    
    #验证:
    xcrun altool --validate-app -f ${exportIpaPath}/${scheme_name}.ipa -t ios --apiKey ${password} --apiIssuer ${username} --verbose
    #上传:
    xcrun altool --upload-app -f ${exportIpaPath}/${scheme_name}.ipa -t ios --apiKey ${password} --apiIssuer ${username} --verbose
else
    #上传到Fir或者蒲公英配置信息
    # 将XXX替换成自己的Fir平台的token
    fir login -T XXX
    fir publish $exportIpaPath/$scheme_name.ipa
    
    #curl -H "Expect:" -F 'file=@test_2020-03-03.txt' http://192.168.100.xx:xxxx/api

fi

exit 0


